package com.banciyuan.bcywebview.biz.setting;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.api.UserApi;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;

import com.banciyuan.bcywebview.utils.http.BcyPair;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by hy on 2015/4/30.
 */
public class SettingGenderDialog extends Dialog {
    public SettingGenderDialog(Context context) {
        super(context);
    }

    public SettingGenderDialog(Context context, int theme) {
        super(context, theme);
    }

    protected SettingGenderDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder {
        private Context context;

        private String gender;

        private ChangeGenderListener changeGenderListener;
        private RequestQueue mQueue;

        public Builder(Context context, String gender) {
            this.context = context;
            this.gender = gender;
            mQueue = VolleyQueue.getRquest(context);
        }


        public void setChangeGenderListener(ChangeGenderListener changeGenderListener) {
            this.changeGenderListener = changeGenderListener;
        }

        public SettingGenderDialog create() {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final SettingGenderDialog mydialog = new SettingGenderDialog(context, R.style.PcHeadDialog);
            View layout = inflater.inflate(R.layout.setting_gender_dialog, null);
            final ImageView ivBoy = (ImageView) layout.findViewById(R.id.iv_boy);
            final ImageView ivGirl = (ImageView) layout.findViewById(R.id.iv_girl);
            if (context.getString(R.string.male).equals(gender)) {
                ivBoy.setVisibility(View.VISIBLE);
                ivGirl.setVisibility(View.INVISIBLE);
            } else {
                ivBoy.setVisibility(View.INVISIBLE);
                ivGirl.setVisibility(View.VISIBLE);
            }
            layout.findViewById(R.id.rl_girl).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //todo
                    ivBoy.setVisibility(View.INVISIBLE);
                    ivGirl.setVisibility(View.VISIBLE);
                    changSex(0, mydialog);
                }
            });
            layout.findViewById(R.id.rl_boy).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //TODO
                    ivBoy.setVisibility(View.VISIBLE);
                    ivGirl.setVisibility(View.INVISIBLE);
                    changSex(1, mydialog);
                }
            });
            layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mydialog.dismiss();
                }
            });
            mydialog.setContentView(layout);
            return mydialog;
        }

        private void changSex(final int i, final SettingGenderDialog mydialog) {
            List<BcyPair> paramsList = new ArrayList<BcyPair>();
            if (UserDataHelper.ifLogin(context))
                paramsList.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(context).getToken()));
            paramsList.add(new BcyPair(HttpUtils.SEX, i + ""));
            final HashMap<String, String> params = HttpUtils.getData(paramsList);
            String url = HttpUtils.BASE_URL + UserApi.getModifysex();

            Response.Listener<String> onSucc = new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {
                    JSONObject result = null;
                    try {
                        result = new JSONObject(s);
                        String st = result.getString(HttpUtils.UPLOAD_STATUS);
                        String data = result.getString(HttpUtils.UPLOAD_DATA);
                        if (st.equals("1")) {
                            switch (i) {
                                case 0:
                                    if (null != changeGenderListener) {
                                        changeGenderListener.onGirlClicked();
                                    }
                                    mydialog.dismiss();
                                    break;
                                case 1:
                                    if (null != changeGenderListener) {
                                        changeGenderListener.onBoyClicked();
                                    }
                                    mydialog.dismiss();
                                    break;
                            }
                            MyToast.show(context, data);
                        } else {
                            MyToast.show(context, data);
                            mydialog.dismiss();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        MyToast.show(context, "修改失败");
                        mydialog.dismiss();
                    }
                }
            };

            Response.ErrorListener onError = new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    mydialog.dismiss();
                }
            };

            StringRequestParam stringRequest = new StringRequestParam(Request.Method.POST, url, params, onSucc, onError);
            mQueue.add(stringRequest);
        }

        public interface ChangeGenderListener {
            void onBoyClicked();

            void onGirlClicked();
        }

    }


}
